package com.it.controller.admin;

import com.it.admin.AdminCategoryService;
import com.it.admin.AdminUserService;
import com.it.bean.R;
import com.it.bean.RestStatusEnum;
import com.it.pojo.Category;
import com.it.pojo.params.BasePage;
import com.it.pojo.params.category.CategoryEditQuery;
import com.it.pojo.params.user.UserEditQuery;
import com.it.pojo.vo.category.AdminCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@CrossOrigin
@RequestMapping("/admin/category")
@Api(value = "v1", tags = "分类相关接口")

public class AdminCategoryController {


    @Resource
    private AdminCategoryService adminCategoryService;

    @GetMapping("list")
    @ApiOperation(value = "查询所有分类", notes = "查询所有分类")
    public R<?> list() {
        List<AdminCategoryVO> adminCategoryVOS = adminCategoryService.listCategories();
        return R.success("成功", adminCategoryVOS);
    }


    @PostMapping("save")
    @ApiOperation(value = "新增分类", notes = "新增分类")
    public R<?> save(@RequestParam("categoryName") String categoryName) {
        if (StringUtils.isEmpty(categoryName)) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        System.out.println("categoryName = " + categoryName);
        return adminCategoryService.insertCategory(categoryName) > 0 ? R.success() : R.error();
    }

    @PostMapping("list")
    @ApiOperation(value = "查询分类", notes = "查询分类")
    public R<?> list(@RequestBody BasePage basePage) {
        Integer pageNum = basePage.getPageNum();
        Integer limit = basePage.getLimit();
        if (pageNum <= 0 || limit <= 0) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        Map<String, Object> map = adminCategoryService.listCategories(basePage);
        return R.success("成功", map);
    }

    @DeleteMapping("{categoryId}")
    @ApiOperation(value = "删除分类", notes = "删除分类")
    public R<?> delete(@PathVariable("categoryId") Integer categoryId) {
        if (categoryId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        return adminCategoryService.deleteCategoryById(categoryId)
                > 0 ? R.success() : R.error();
    }

    @PutMapping("")
    @ApiOperation(value = "根据分类id修改分类信息", notes = "根据分类id修改分类信息")
    public R<?> edit(@RequestBody CategoryEditQuery categoryEditQuery) {
        if (categoryEditQuery == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        // TODO 参数校验不做
        Category category = new Category();
        BeanUtils.copyProperties(categoryEditQuery, category);
        return adminCategoryService.updateCategoryById(category)
                > 0 ? R.success() : R.error();
    }

    @GetMapping("{categoryId}")
    public R<?> getCategory(@PathVariable("categoryId") Integer categoryId) {
        if (categoryId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        String rest = adminCategoryService.getCategory(categoryId);
        return R.success("成功", rest);
    }
}
